package com.siebre.picclife.asynctask.service.support;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.integration.core.MessagingOperations;
import org.springframework.integration.support.MessageBuilder;

import com.siebre.picclife.asynctask.service.TaskManager;

/**
 * 
 * @author ZhangChi
 * @since 2015年4月9日
 *
 */
public class DefaultTaskManager implements TaskManager {
	
	private Logger logger = LoggerFactory.getLogger(getClass());
	
	private final MessagingOperations messagingOps;

	public DefaultTaskManager(MessagingOperations messagingOps) {
		this.messagingOps = messagingOps;
	}

	public void start() {
		messagingOps.send("controlChannel", MessageBuilder.withPayload("@'task-processor'.start()").build());
		messagingOps.send("controlChannel", MessageBuilder.withPayload("@'task-processor-retry'.start()").build());
		logger.info("task processing started");
	}

	public void stop() {
		messagingOps.send("controlBus", MessageBuilder.withPayload("@'task-processor'.stop()").build());
		messagingOps.send("controlBus", MessageBuilder.withPayload("@'task-processor-retry'.stop()").build());
		logger.info("task processing stopped");
	}
}
